package common

import (
	"encoding/json"
)

type R struct {
	Code int               `json:"code"`
	Msg  string            `json:"msg"`
	Data map[string]interface{} `json:"data,omitempty"`
}

const SUCCESS = 200

func R_Ok(msg string) *R {
	return &R{
		Code: SUCCESS,
		Msg:  msg,
	}
}

//func R_Ok_Result(data map[string]string) string {
//	r := R{Code: SUCCESS, Msg: "success", Data: data}
//	str, _ := json.Marshal(r)
//	return string(str)
//}

func R_Ok_Result(data map[string]interface{}) string {
	r := R{Code: SUCCESS, Msg: "success", Data: data}
	str, _ := json.Marshal(r)
	return string(str)
}

func R_Error_Result(msg string) string {
	r := R{Code: ERROR, Msg: msg}
	str, _ := json.Marshal(r)
	return string(str)
}

func IsOk(r *R) string {
	if r.Code == SUCCESS {
		return ""
	}
	str , _:=json.Marshal(r)
	return string(str)
}

const ERROR = 300

func R_Errors(msg string) *R {
	return &R{
		Code: ERROR,
		Msg:  msg,
	}
}

func R_NewError(code int, msg string) *R {
	return &R{
		Code: code,
		Msg:  msg,
	}
}
